import type { CaseReducer, PayloadAction } from '@reduxjs/toolkit';
import { createSlice } from '@reduxjs/toolkit';
type TSafeEduStateToParent = {
  name: 'micro-safe-edu';
  type: string; //'childHasLoaded';
  data?: any;
};

type OnGlobalStateChangeCallback = (state: Record<string, any>, prevState: Record<string, any>) => void;
export type MicroAppSliceType = {
  onGlobalStateChange?: (callback: OnGlobalStateChangeCallback, fireImmediately?: boolean) => void;
  setGlobalState?: (state: TSafeEduStateToParent) => boolean;
  offGlobalStateChange?: () => boolean;
  [key: string]: any;
};

export const microAppSlice = createSlice<
  MicroAppSliceType,
  {
    updateMicroAppStore: CaseReducer<MicroAppSliceType, PayloadAction<MicroAppSliceType | undefined>>;
  },
  'microAppStore'
>({
  name: 'microAppStore',
  initialState: () => {
    return {};
  },
  reducers: {
    updateMicroAppStore: (state, { payload = {} }) => {
      // console.log("---microAppSlice--state---:", state)
      return {
        ...state,
        ...payload,
      };
    },
  },
});
